<!DOCTYPE HTML>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport"
        content="maximum-scale=1.0, minimum-scale=1.0, user-scalable=0, initial-scale=1.0, width=device-width" />
    <meta name="format-detection" content="telephone=no, email=no, date=no, address=no">
    <title>Hello APP</title>
    <link rel="stylesheet" type="text/css" href="../css/api.css" />
    <style>

    </style>
</head>

<body>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/service/service-tool.js"></script>
<script type="text/javascript" src="../script/service/service-app.js"></script>
<script type="text/javascript" src="../script/service/service-user.js"></script>
<script type="text/javascript" src="../script/service/service-logic.js"></script>
<script type="text/javascript" src="../script/jquery.min.js"></script>
<script type="text/javascript" src="../script/vue.min.js"></script>
<script type="text/javascript">
    var tabFrame = ["tab_index_frms", "", "", ""];
    var ajpush;
    var USER;
    var $S;
    apiready = function () {
        USER = USER();
        $S = SERVICE();
        localStorage.safeTop = api.safeArea.top; //获取顶部状态栏的高度
        localStorage.safeBottom = api.safeArea.bottom; //获取底部栏的高度
        api.setPrefs({
            key: 'safeTop',
            value: api.safeArea.top
        });
        api.setPrefs({
            key: 'safeBottom',
            value: api.safeArea.bottom
        });
        //2次点击返回按钮监听事件
        ExitAppByKeyBack2();
        api.addEventListener({
            name: 'tabitembtn'
        }, function (ret) {
            var index = ret.index;
            fnShowBarFrm(index)
        });
        ajpush = api.require('ajpush');
        var systemType = api.systemType
        if (systemType != 'ios') {
            ajpush.init(function (ret) {
                if (ret && ret.status) {
                    fnBindTag();
                }
            });
        } else {
            fnBindTag()
        }
        api.addEventListener({
            name: 'loginStatus'
        }, function (ret, err) {
            fnBindTag();
            var custId = USER.Get('custId', true);
            if (custId && custId.length > 0) {
                api.sendEvent({
                    name: 'updateUserInfo'
                });
                var userType = getUserType();
                if (userType == 1) {
                    getClassId();
                }
            }
        });
        setTimeout(function () {
            fnShowBarFrm(0)
        }, 1000 * 0.8)
        var userType = getUserType();
        if (userType == 1) {
            getClassId();
        }
    }

    function getClassId() {
        var params = {};
        $S.POST('clsClass/findClassesList', params, function (ret) {
            if (ret.status == 200 && ret.data.listData.length > 0) {
                USER.Set({ 'classId': ret.data.listData[0].id });
            };
        });
    }

    function fnShowBarFrm(index) {
        var custId = USER.Get('custId', true);
        // if(index == 1 && !custId) {
        //     fnOpenWin({
        //         name: 'login',
        //         url: './login/login.html'
        //     });
        //     return;
        // }
        if (index == 2) {
            const url = "https://shop98220230.m.youzan.com/wscshop/showcase/feature?alias=QPOMQr3MUm&kdt_id=98028062";
            api.openApp({
                iosUrl: url,
                uri: url,
            });


            // var params = {
            //     name: 'shop_mall_win',
            //     url: './tabs/tab_shop.html',
            //     slidBackEnabled: false,
            //     reload: false,
            //     animation: {
            //         type: 'movein',
            //         subType: 'from_bottom',
            //         duration: 500
            //     }
            // }
            // fnOpenWin(params);
            // api.sendEvent({
            //     name: 'backToIndex'
            // });
        } else {
            api.setTabBarAttr({
                index: index
            });
            api.setFrameGroupAttr({
                name: 'tab_index_frms',
                hidden: index == 0 ? false : true
            });
            if (index == 4) {
                api.sendEvent({
                    name: 'updateUserInfo'
                });
            }
        }

    }

    function fnBindTag() {
        var _tag = api.getPrefs({
            sync: true,
            key: 'pushTag'
        }) || "";
        if (fnCheckIsOpenLogin()) {
            if (_tag != "1") {
                _tag = USER.Get('custId', true) || "";
            }
        }
        api.setPrefs({
            key: 'pushTag',
            value: _tag
        });
        var param = {
            alias: _tag,
            tags: ['tag1', 'tag2']
        };
        ajpush.bindAliasAndTags(param, function (ret) {
            if (ret.statusCode == "6002") {
                setTimeout(function () {
                    fnBindTag();
                }, "2000")
            }
        });
        fnMessage();
    }

    function fnMessage() {
        var systemType = api.systemType;
        if (systemType == 'ios') {
            //后台运行监听
            ajpush.setListener(function (ret) {
                if (ret.extra.ext) {
                    var _pushValue = ret.extra.ext;
                    fnRecievePush(_pushValue);
                }
                api.notification({
                    notify: {
                        title: "",
                        content: ret.content
                    }
                }, function (ret, err) {
                    return;
                });
            });
            api.addEventListener({
                name: 'noticeclicked'
            }, function (ret, err) {
                if (ret && ret.value) {
                    var _pushValue = ret.value.extra.ext;
                    fnRecievePush(_pushValue);
                }
            })
        } else {
            api.addEventListener({
                name: 'appintent'
            }, function (ret, err) {
                if (ret && ret.appParam.ajpush) {
                    var _push = ret.appParam.ajpush.extra.ext;
                    fnRecievePush(_push);
                }
            })
        }
    }

    function fnRecievePush(sender) {
        // alert(sender+'======fnRecievePush');
        sender = JSON.parse(sender || '{}');
        var messageId = sender.messageInfoId;
        var messageCode = sender.messageInfoTypeCode;
        var detailId = sender.messageInfoBusinessId;
        fnOpenMessageDetail(messageId, messageCode, detailId)
    }

    //消息推送的点击事件
    function fnOpenMessageDetail(id, messageCode, detailId) {
        messageCode = ',' + messageCode + ',';
        // alert(messageCode+'=========messageCode===id==='+id);
        if (',1,12,'.indexOf(messageCode) !== -1) {
            // 系统消息
            var code = messageCode.replace(',', '');
            fnOpenPublicWin('消息详情', 'teacher/info_detail', {
                messageInfoId: code == 12 ? detailId : id,
                msgType: code
            });
        } else if (',4,5,'.indexOf(messageCode) !== -1) {
            // 老师视频审核消息
            fnOpenPublicWin('我的上传', 'teacher/my_upload', {
                isBtn: true,
                btnName: '管理',
                headerFn: 'headerFn'
            });
        } else if (',6,7,8,10,11,'.indexOf(messageCode) !== -1) {
            // 订单消息
            // 7 8 售后订单
            fnOpenPublicWin('订单详情', 'teacher/order_detail', {
                status: ',7,8,'.indexOf(messageCode) !== -1 ? 20 : '',
                id: detailId,
                orderIndex: -1
            });
        } else if (',9,'.indexOf(messageCode) !== -1) {
            // 视频订单
            fnOpenPublicWin('视频订单', 'teacher/video_order');
        }
    }
</script>

</html>